﻿using System;
namespace Common
{
	public class Timing
	{
		public Timing()
		{
			Year = -1;
			Month = -1;
			Day = -1;
			Hour = -1;
			Minute = -1;
			Second = -1;
		}

		public Timing(int year, int month, int day, int hour, int minute, int second)
			:this()
		{
			Year = year;
			Month = month;
			Day = day;
			Hour = hour;
			Minute = minute;
			Second = second;
		}

		public int Year { get; set; }
		public int Month { get; set; }
		public int Day { get; set; }
		public int Hour { get; set; }
		public int Minute { get; set; }
		public int Second { get; set; }

		public DateTime NextTiming()
		{
			DateTime dt = DateTime.Now;

			var list = new string[] { "Second", "Minute", "Hour", "Day", "Month", "Year" };
			foreach (var item in list)
			{
				var info = typeof(DateTime).GetProperty(item);
				var value_info = typeof(Timing).GetProperty(item);
				var value = value_info.GetValue(this);
				var method = typeof(DateTime).GetMethod("Add" + item + "s");
				var now_value = info.GetValue(DateTime.Now);
				if ((int)value != -1)
				{
					dt = (DateTime)method.Invoke(dt, new object[] { (int)value - (int)now_value });
				}
				else
				{
					dt = (DateTime)method.Invoke(dt, new object[] { 1 });
					break;
				}
			}

			return dt;
		}
	}
}
